package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.GreTreeCuringTechnologyLogic;
import cn.topevery.garden.entity.dto.GreTreeCuringTechnologyDTO;
import cn.topevery.garden.entity.po.GreTreeCuringTechnology;
import cn.topevery.garden.entity.query.GreTreeCuringTechnologyQuery;
import cn.topevery.garden.entity.vo.GreTreeCuringTechnologyVO;
import com.github.dozermapper.core.Mapper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 古树名木养护技术表
 *
 * @author code-generate
 * @date 2022-02-27
 */
@Api(tags = "古树名木养护技术表")
@RestController
@RequestMapping("/greTreeCuringTechnology")
public class GreTreeCuringTechnologyController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    GreTreeCuringTechnologyLogic greTreeCuringTechnologyLogic;


    /**
     * 获取古树名木养护技术表分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取古树名木养护技术表页列表")
//    @PostMapping("/listPage")
    @PostMapping("/getList")
    public JsonResult<DataPager<GreTreeCuringTechnologyVO>> listPage(@RequestBody GreTreeCuringTechnologyQuery query) {
        return JsonResult.ok(greTreeCuringTechnologyLogic.listPage(query));
    }

    @ApiOperation("新增/编辑 古树名木养护技术表信息")
//    @PostMapping("/saveOrUpdate")
    @PostMapping("/save")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody GreTreeCuringTechnologyDTO dto) {
        String userId = getUser().id.toString();
        GreTreeCuringTechnology greTreeCuringTechnology = mapper.map(dto, GreTreeCuringTechnology.class);
        greTreeCuringTechnologyLogic.saveOrUpdate(userId,greTreeCuringTechnology);
        return JsonResult.ok(true);
    }

    @ApiOperation("通过Id获取详情")
//    @GetMapping("/getInfoById")
    @GetMapping("/getById")
    public JsonResult<GreTreeCuringTechnologyVO> getInfoById(@RequestParam Integer id) {
        GreTreeCuringTechnology entity = greTreeCuringTechnologyLogic.getEntity(id);
        GreTreeCuringTechnologyVO vo= mapper.map(entity, GreTreeCuringTechnologyVO.class);
        return JsonResult.ok(vo);
    }

    @ApiOperation("根据自增ID批量删除")
//    @PostMapping("/deleteBatchById")
    @GetMapping("/delete")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        greTreeCuringTechnologyLogic.deleteBatchById(getUser().id.toString(),ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出古树名木养护技术表信息")
    @GetMapping("/export")
    public void export(GreTreeCuringTechnologyQuery query, HttpServletResponse response) {
        List<GreTreeCuringTechnologyVO> list = greTreeCuringTechnologyLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "古树名木养护技术表", "sheet1", GreTreeCuringTechnologyVO.class, "古树名木养护技术表", response);
    }

}
